home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / FileOutputStream.java < prev    next >
Text File  |  1998-09-22  |  8KB  |  227 lines

  1. /*
  2.  * @(#)FileOutputStream.java    1.27 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16. import java.io.File;
  17.  
  18. /**
  19.  * A file output stream is an output stream for writing data to a 
  20.  * <code>File</code> or to a <code>FileDescriptor</code>. 
  21.  *
  22.  * @author  Arthur van Hoff
  23.  * @version 1.27, 07/01/98
  24.  * @see     java.io.File
  25.  * @see     java.io.FileDescriptor
  26.  * @see     java.io.FileInputStream
  27.  * @since   JDK1.0
  28.  */
  29. public
  30. class FileOutputStream extends OutputStream
  31. {
  32.     /**
  33.      * The system dependent file descriptor. The value is
  34.      * 1 more than actual file descriptor. This means that
  35.      * the default value 0 indicates that the file is not open.
  36.      */
  37.     private FileDescriptor fd;
  38.  
  39.     /**
  40.      * Creates an output file stream to write to the file with the 
  41.      * specified name. 
  42.      *
  43.      * @param      name   the system-dependent filename.
  44.      * @exception  IOException  if the file could not be opened for writing.
  45.      * @exception  SecurityException  if a security manager exists, its
  46.      *               <code>checkWrite</code> method is called with the name
  47.      *               argument to see if the application is allowed write access
  48.      *               to the file.
  49.      * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
  50.      * @since      JDK1.0
  51.      */
  52.     public FileOutputStream(String name) throws IOException {
  53.     SecurityManager security = System.getSecurityManager();
  54.     if (security != null) {
  55.         security.checkWrite(name);
  56.     }
  57.     try {
  58.         fd = new FileDescriptor();
  59.         open(name);
  60.     } catch (IOException e) {
  61.         throw new FileNotFoundException(name);
  62.     }
  63.     }
  64.  
  65.     /**
  66.      * Creates an output file with the specified system dependent
  67.      * file name.
  68.      * @param name the system dependent file name 
  69.      * @exception IOException If the file is not found.
  70.      * @since     JDK1.1
  71.      */
  72.     public FileOutputStream(String name, boolean append) throws IOException {
  73.     SecurityManager security = System.getSecurityManager();
  74.     if (security != null) {
  75.         security.checkWrite(name);
  76.     }
  77.     try {
  78.         fd = new FileDescriptor();
  79.         if(append)
  80.         openAppend(name);
  81.         else
  82.         open(name);
  83.     } catch (IOException e) {
  84.         throw new FileNotFoundException(name);
  85.     }
  86.     }
  87.     
  88.     /**
  89.      * Creates a file output stream to write to the specified 
  90.      * <code>File</code> object. 
  91.      *
  92.      * @param      file   the file to be opened for writing.
  93.      * @exception  IOException        if the file could not be opened for
  94.      *               writing.
  95.      * @exception  SecurityException  if a security manager exists, its
  96.      *               <code>checkWrite</code> method is called with the pathname
  97.      *               of the <code>File</code> argument to see if the
  98.      *               application is allowed write access to the file. This may
  99.      *               result in a security exception.
  100.      * @see        java.io.File#getPath()
  101.      * @see        java.lang.SecurityException
  102.      * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
  103.      * @since      JDK1.0
  104.      */
  105.     public FileOutputStream(File file) throws IOException {
  106.     this(file.getPath());
  107.     }
  108.  
  109.     /**
  110.      * Creates an output file stream to write to the specified file descriptor.
  111.      *
  112.      * @param      fdObj   the file descriptor to be opened for writing.
  113.      * @exception  SecurityException  if a security manager exists, its
  114.      *               <code>checkWrite</code> method is called with the file
  115.      *               descriptor to see if the application is allowed to write
  116.      *               to the specified file descriptor.
  117.      * @see        java.lang.SecurityManager#checkWrite(java.io.FileDescriptor)
  118.      * @since      JDK1.0
  119.      */
  120.     public FileOutputStream(FileDescriptor fdObj) {
  121.     SecurityManager security = System.getSecurityManager();
  122.     if (fdObj == null) {
  123.         throw new NullPointerException();
  124.     }
  125.     if (security != null) {
  126.         security.checkWrite(fdObj);
  127.     }
  128.     fd = fdObj;
  129.     }
  130.  
  131.     /**
  132.      * Opens a file, with the specified name, for writing.
  133.      * @param name name of file to be opened
  134.      */
  135.     private native void open(String name) throws IOException;
  136.  
  137.     /**
  138.      * Opens a file, with the specified name, for appending.
  139.      * @param name name of file to be opened
  140.      */
  141.     private native void openAppend(String name) throws IOException;
  142.  
  143.     /**
  144.      * Writes the specified byte to this file output stream. 
  145.      *
  146.      * @param      b   the byte to be written.
  147.      * @exception  IOException  if an I/O error occurs.
  148.      * @since      JDK1.0
  149.      */
  150.     public native void write(int b) throws IOException;
  151.  
  152.     /**
  153.      * Writes a sub array as a sequence of bytes.
  154.      * @param b the data to be written
  155.      * @param off the start offset in the data
  156.      * @param len the number of bytes that are written
  157.      * @exception IOException If an I/O error has occurred.
  158.      */
  159.     private native void writeBytes(byte b[], int off, int len) throws IOException;
  160.  
  161.     /**
  162.      * Writes <code>b.length</code> bytes from the specified byte array 
  163.      * to this file output stream. 
  164.      *
  165.      * @param      b   the data.
  166.      * @exception  IOException  if an I/O error occurs.
  167.      * @since      JDK1.0
  168.      */
  169.     public void write(byte b[]) throws IOException {
  170.     writeBytes(b, 0, b.length);
  171.     }
  172.  
  173.     /**
  174.      * Writes <code>len</code> bytes from the specified byte array 
  175.      * starting at offset <code>off</code> to this file output stream. 
  176.      *
  177.      * @param      b     the data.
  178.      * @param      off   the start offset in the data.
  179.      * @param      len   the number of bytes to write.
  180.      * @exception  IOException  if an I/O error occurs.
  181.      * @since      JDK1.0
  182.      */
  183.     public void write(byte b[], int off, int len) throws IOException {
  184.     writeBytes(b, off, len);
  185.     }
  186.  
  187.     /**
  188.      * Closes this file output stream and releases any system resources 
  189.      * associated with this stream. 
  190.      *
  191.      * @exception  IOException  if an I/O error occurs.
  192.      * @since      JDK1.0
  193.      */
  194.      public native void close() throws IOException;
  195.  
  196.      /**
  197.       * Returns the file descriptor associated with this stream.
  198.      *
  199.      * @return  the file descriptor object associated with this stream.
  200.      * @exception  IOException  if an I/O error occurs.
  201.      * @see        java.io.FileDescriptor
  202.      * @since      JDK1.0
  203.       */
  204.      public final FileDescriptor getFD()  throws IOException {
  205.     if (fd != null) return fd;
  206.     throw new IOException();
  207.      }
  208.     
  209.     /**
  210.      * Ensures that the <code>close</code> method of this file output stream is
  211.      * called when there are no more references to this stream. 
  212.      *
  213.      * @exception  IOException  if an I/O error occurs.
  214.      * @see        java.io.FileInputStream#close()
  215.      * @since      JDK1.0
  216.      */
  217.     protected void finalize() throws IOException {
  218.      if (fd != null) {
  219.          if (fd == fd.out || fd == fd.err) {
  220.          flush();
  221.          } else {
  222.          close();
  223.          }
  224.      }
  225.     }
  226. }
  227.